<?php
declare(strict_types=1);

namespace Service\Consumer\YunCangConsumer\Router;
use Service\BaseService;
use App\Model\BbcModel\Goods;
use Service\EsService\EsPublishMsgService;
use Service\GoodsService\ChannelGoodsService;
use Service\SearchService\Index\ChannelCommodityIndex;
use Service\GoodsService\MnGoodsService;
use App\Model\BbcModel\MnStoreGoods;
use Service\SearchService\Index\StoreCommodityIndex;
use Service\Code\Normal;

/**
 * 商品批量下架
 * @author chenshubo
 */
class goodsSkuChange extends BaseService
{
    /**
     * 接收消费数据
     * @param int $sid
     * @param int $cid
     * @param array $data
     * @param string $messageId
     * @param int $IsFinished
     */
    public function consumer(int $sid,int $cid,array $data,string $messageId,int $IsFinished)
    {
        try {

            $skuCodes = $data['skuList'];
            $dataList = ChannelGoodsService::instance()->queryGoodsListBySkuCodeAndCid($cid,$skuCodes);
            $goodsIds = array_column($dataList,'id');
            if( $goodsIds ) 
            {
                //渠道搜索商品服务删除
                Goods::instance()->whereIn('id',$goodsIds)->update(['df'=>1,'md'=>time()]);
                EsPublishMsgService::instance()->pushNew($goodsIds,'channel','delete');
                //店铺搜索商品服务删除
                $mnGoodsList = MnGoodsService::instance()->queryMnGoodsListByGoodsIds($goodsIds);
                if( $mnGoodsList )
                {
                    $chunk_mngoods_list = array_chunk($mnGoodsList,self::CHUNK_SIZE);
                    foreach ($chunk_mngoods_list as $chunk_data)
                    {
                        $mn_goods_data_ids = array_column($chunk_data,'id');
                        EsPublishMsgService::instance()->pushNew($mn_goods_data_ids,'store','delete');
                        MnStoreGoods::instance()->whereIn('id',$mn_goods_data_ids)->update(['df'=>1,'md'=>time()]);
                    }
                }
            }
            return Normal::returnCode(Normal::SUC,"处理完成");
        } catch ( \Throwable $th ) {
            $error = [
                'error'         => $th->getMessage(),
                'errorFile'     => $th->getFile(),
                'errorLine'     => $th->getLine(),
            ];
            $_class_name = __CLASS__;
            return Normal::returnCode(Normal::FAIL,"{$_class_name}消费处理处理异常【{$error['error']}】",$error);
        }
    }
}